package com.example.myspringbootproject.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.myspringbootproject.mapper.TeacherMapper;
import com.example.myspringbootproject.pojo.T_teacher;
import com.example.myspringbootproject.service.impl.TeacherServiceImpl;
import com.example.myspringbootproject.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/exam/teacher")
public class teacherController {
    @Autowired
    @Lazy
    TeacherServiceImpl teacherService;
    private T_teacher three;
    @Autowired
    TeacherMapper teacherMapper;
    private T_teacher t_teacher;
    /**
     * 获取用户列表
     * 查询以及分页
     */
    @GetMapping(value = "/list")

    public Result list(int currentPage,int pageSize,String content) {
        System.out.println("当前页："+currentPage+"查询条数："+pageSize+"查询条件"+content);
        //条件查询
        QueryWrapper<T_teacher> qw = new QueryWrapper<>();
        //判断是否按性别查询
        if (content.equals("男") || content.equals("女")){
            qw.eq("sex",content.equals("男")? "男" : "女");
        }else {
            qw.like("username",content).or().like("name",content).or().like("title",content);
        }


        Page<T_teacher> page = new Page<>(currentPage,pageSize);
        IPage<T_teacher> page1 = teacherService.page(page,qw);


        Map<String, Object> map = new HashMap<>();
        map.put("list", page1.getRecords());
        map.put("total", page1.getTotal());

        return Result.ok(map);

    }
    //新增
    @PostMapping(value = "/addUser")
    public Result addUser(@RequestBody T_teacher t_teacher){
        System.out.println("添加的数据是："+ t_teacher);
        T_teacher dbTeacher = teacherMapper.selectByUsername(t_teacher.getUsername());
        //将添加对象的用户信息补充完整(写死)
        t_teacher.setRole("TEACHER");
        if (ObjectUtils.isEmpty(t_teacher.getPassword())){
            t_teacher.setPassword("123456");
        }
        t_teacher.setRole("TEACHER");
        //执行添加操作
        boolean save = teacherService.save(t_teacher);
        return save?Result.ok():Result.fail();
    }

    //删除
    @GetMapping (value = "/delete")
    public Result delete(@RequestParam List<Long> ids) {
        System.out.println("ids==="+ids);

        boolean b=teacherService.removeBatchByIds(ids);
        return b ? Result.ok() : Result.fail();

    }

    //修改
    @PostMapping(value = "/updateUser")
    public Result updateUser(@RequestBody T_teacher t_teacher){
        System.out.println("修改的数据是："+ t_teacher);
        //将添加对象的用户信息补充完整
        boolean update = teacherService.updateById(t_teacher);
        //执行添加操作

        return update ? Result.ok():Result.fail();
    }


}
